package com.framework.core.mybatis;

import com.framework.core.mybatis.config.MybatisDdlProperties;
import com.framework.core.mybatis.context.TableContext;
import com.framework.core.mybatis.ddl.IMybatisDdlFactory;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

/**
 *  入口类
 *  created by zzg
 *  2024-12-06
 */
@Component
@Slf4j
public class MybatisDdlInitListener implements ApplicationListener<ContextRefreshedEvent> {
    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        ApplicationContext applicationContext = event.getApplicationContext();
        IMybatisDdlFactory factory = applicationContext.getBean(IMybatisDdlFactory.class);
        MybatisDdlProperties properties = applicationContext.getBean(MybatisDdlProperties.class);
        SqlSessionFactory sqlSession = applicationContext.getBean(SqlSessionFactory.class);
        TableContext context = new TableContext(sqlSession, factory, properties);
        context.init();
    }
}
